package pt.ipleiria.estg.es2.byinvitationonly;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import static android.provider.ContactsContract.Contacts;
import static android.provider.ContactsContract.Profile;
import static pt.ipleiria.estg.es2.byinvitationonly.WhoIsHereActivity.IS_CHECKED_IN;


public class MainActivity extends Activity {

    private boolean isChecked = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_i_am_here) {
            //verificar si el contacto no esta vacio
            String[] mProjection;
            mProjection = new String[]
                    {
                            Profile._ID,
                            Profile.DISPLAY_NAME_PRIMARY,
                            Profile.LOOKUP_KEY,
                            Profile.DISPLAY_NAME_ALTERNATIVE,
                            Profile.DISPLAY_NAME_SOURCE,
                    };
            Uri uri = Uri.withAppendedPath(Profile.CONTENT_URI, Contacts.Data.CONTENT_DIRECTORY);
            Log.d("User", "getOwner: " + uri);
            Cursor cursor;
            cursor = getContentResolver().query(uri, mProjection, null, null, null);
            Log.d("Cursor", String.valueOf(cursor.getCount()));

            //4 linhas 3 colunas
/*            -----------------------------------------------
            | _ID | DISPLAY_NAME_PRIMARY |   LOOKUP_KEY   |
            | --------------------------------------------|
            |     |                      |                |
            |---------------------------------------------|
            |     |                      |                |
            |---------------------------------------------|
            |     |                      |                |
            ----------------------------------------------|
            |     |                      |                |
            |---------------------------------------------|*/

            if (cursor.getCount() > 0) {
                cursor.moveToPosition(-1);
                while (cursor.moveToNext()) {
                    Log.d("Cursor", cursor.getString(0) + " - " + cursor.getString(1) + " - " + cursor.getString(2) + " - " + cursor.getString(3) + " - " + cursor.getString(4));
                }
            }

            if (cursor.getCount() > 0) {
                //mensagem de confirmacao
                AlertDialog alertDialog;
                alertDialog = new AlertDialog.Builder(this, AlertDialog.THEME_HOLO_DARK).create();
                alertDialog.setTitle("Verificação de inscrição");
                alertDialog.setMessage("Tem certeza de que quer continuar?");
                alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Sim", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Não", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(), "Operação cancelada", Toast.LENGTH_SHORT).show();
                    }
                });
                alertDialog.show();
            } else {
                //abrir janela para preencher o contato
            }
            //verificar si el dispositivo tiene red

            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void invertCheckIn(MenuItem item) {
        isChecked = !isChecked;
        item.setIcon(isChecked ? R.drawable.ic_action_networking : R.drawable.ic_action_alone);

        if (isChecked) {
            Intent intent = new Intent(this, WhoIsHereActivity.class);
            intent.putExtra(IS_CHECKED_IN, isChecked);
            startActivity(intent);
        }
    }
}
